package lt.marsrutai.mobile.android.widget;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Represents a dialog choice item
 * 
 * @author Justas
 */
public class DialogChoiceItem implements Parcelable, Cloneable {

    public static final Parcelable.Creator<DialogChoiceItem> CREATOR = new Parcelable.Creator<DialogChoiceItem>() {

        @Override
        public DialogChoiceItem createFromParcel(Parcel in) {
            return new DialogChoiceItem(in);
        }

        @Override
        public DialogChoiceItem[] newArray(int size) {
            return new DialogChoiceItem[size];
        }
    };

    /** unique item label resource id */
    private int nameResourceId;

    /** Indicated that this item may be displayed only once in multiple dialogs */
    private boolean singleSelection;

    /** The count of times this item is used throughout various dialogs */
    private int usedCount = 0;

    public DialogChoiceItem(int label, boolean singleSelection) {
        this.nameResourceId = label;
        this.singleSelection = singleSelection;
    }

    private DialogChoiceItem(Parcel in) {
        nameResourceId = in.readInt();
        singleSelection = in.readByte() == 1;
        usedCount = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(nameResourceId);
        dest.writeByte((byte) (singleSelection ? 1 : 0));
        dest.writeInt(usedCount);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * Item is usable (can be displayed as a choice) when:
     * <ul>
     * <li>single selection - is not used at the moment (
     * <code>usedCount == 0</code>)</li>
     * <li>multiple selection - always</li>
     * </ul>
     * 
     * @return
     */
    public boolean isUsable() {
        return singleSelection ? usedCount == 0 : true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + nameResourceId;
        result = prime * result + (singleSelection ? 1231 : 1237);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        DialogChoiceItem other = (DialogChoiceItem) obj;
        if (nameResourceId != other.nameResourceId) {
            return false;
        }
        if (singleSelection != other.singleSelection) {
            return false;
        }
        return true;
    }
    
    @Override
    public DialogChoiceItem clone() {
        try {
            return (DialogChoiceItem) super.clone();
        }
        catch (CloneNotSupportedException e) {
            return null;
        }
    }

    public int getNameResourceId() {
        return nameResourceId;
    }

    public boolean isSingleSelection() {
        return singleSelection;
    }

    public int getUsedCount() {
        return usedCount;
    }

    public void incUsedCount() {
        usedCount++;
    }

    public void decUsedCount() {
        usedCount--;
    }

}